package com.wdh.kjs.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.provider.DocumentsContract;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import com.wdh.kjs.R;
import com.wdh.kjs.activity.InvestmentDetailActivity;
import com.wdh.kjs.activity.LoginActivity;
import com.wdh.kjs.activity.TupianActivity;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel;
import com.wdh.kjs.util.ToastUtils;

import org.w3c.dom.Document;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by codingWw on 2016/3/7.
 */
public class InvestmentDetailThreeFragment extends Fragment implements View.OnClickListener {
    private TextView tv_idt_companyInformation;
    private JsonModel jsonModel;
    private TextView tv_idt_payment;
    private LinearLayout zoupf_linear;
    private WebView frage_investdetail_text3;
    private LinearLayout ii_fg_investdetail;
    private LinearLayout ii_fg_investdetail_gone;
    private TextView tv_fg_weidenglu;
    private Intent intent;
    private View viewLayout;
  
    private TextView tupian;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_investmentdetail_three, container, false);
        tv_idt_companyInformation = (TextView) view.findViewById(R.id.tv_idt_companyInformation);
        tv_idt_payment = (TextView)view.findViewById(R.id.tv_idt_payment);
        zoupf_linear = (LinearLayout) view.findViewById(R.id.zoupf_linear);
        frage_investdetail_text3 = (WebView) view.findViewById(R.id.frage_investdetail_text3);
        ii_fg_investdetail = (LinearLayout) view.findViewById(R.id.ii_fg_investdetail);
        ii_fg_investdetail_gone = (LinearLayout) view.findViewById(R.id.ii_fg_investdetail_gone);
        tv_fg_weidenglu = (TextView) view.findViewById(R.id.tv_fg_weidenglu);
        if (InvestmentDetailActivity.loginToken.equals("")){
            ii_fg_investdetail.setVisibility(View.GONE);
            ii_fg_investdetail_gone.setVisibility(View.VISIBLE);
            tv_fg_weidenglu.setOnClickListener(this);
        }else {
            intweb(inflater);
        }
        return view;
    }

    private void intweb(final LayoutInflater inflater) {
        RequestParams params = new RequestParams();

        params.addBodyParameter("itemId", InvestmentDetailActivity.itemId);
        params.addBodyParameter("page", 1 + "");
        params.addBodyParameter("rows", "1");
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_Invest_DETAIL, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {


                Gson gson = new Gson();
                jsonModel = gson.fromJson(responseInfo.result, JsonModel.class);

                if (jsonModel.success) {

                    item(inflater);
                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

                ToastUtils.showShort(getActivity(), "网络异常！");
            }
        });

    }

    private void item(LayoutInflater inflater) {
        tv_idt_companyInformation.setText(jsonModel.getItem().getGdFinancingRequired());
        tv_idt_payment.setText(jsonModel.getItem().getGdSrcOfRepayment());
        for (int i=0;i<jsonModel.getItemDocuments().size();i++) {
             viewLayout = inflater.inflate(R.layout.zpf_layout, null);
            tupian=((TextView) viewLayout.findViewById(R.id.zpf_text));
            if (i % 2 == 0) {
                ((LinearLayout) viewLayout.findViewById(R.id.ll_invest_item)).setBackgroundColor(Color.parseColor("#ffffff"));
            }
            else
                ((LinearLayout) viewLayout.findViewById(R.id.ll_invest_item)).setBackgroundColor(Color.parseColor("#eeeeee"));
            final int finalI = i;
if (jsonModel.getItemDocuments().get(i).getIfHrefValid().equals("1")) {
    tupian.setTextColor(getResources().getColor(R.color.colorEmphasis));
    tupian.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (jsonModel.getItemDocuments().get(finalI).getDocumentName().contains("pdf")) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(Httpurl.BadseHOST + jsonModel.getItemDocuments().get(finalI).getItemInfoMaterialPath());
                intent.setData(content_url);
                startActivity(intent);
            }

            else{
                Intent intent = new Intent(getActivity(), TupianActivity.class);
            intent.putExtra("tupian", jsonModel.getItemDocuments().get(finalI).getItemInfoMaterialPath());
            startActivity(intent);
            }

        }
    });
}
            tupian.setText(jsonModel.getItemDocuments().get(i).getDocumentName());


            zoupf_linear.addView(viewLayout);
            if (jsonModel.getItemOtherInfo().size() != 0) {
//                if (jsonModel.getItemOtherInfo().get(0).getItemInfoType() == 1)
//                    frage_investdetail_text3.setText(Html.fromHtml(jsonModel.getItemOtherInfo().get(0).getItemOtherContext()));
//                else
//                    frage_investdetail_text3.setText(Html.fromHtml(jsonModel.getItemOtherInfo().get(1).getItemOtherContext()));
                for (int j=0;j<jsonModel.getItemOtherInfo().size();j++){
                    if (jsonModel.getItemOtherInfo().get(j).getItemInfoType() == 2){
//                        frage_investdetail_text3.setText(Html.fromHtml(jsonModel.getItemOtherInfo().get(j).getItemOtherContext()));
                        frage_investdetail_text3.loadDataWithBaseURL(null,jsonModel.getItemOtherInfo().get(j).getItemOtherContext(), "text/html", "utf-8", null);
                        frage_investdetail_text3.getSettings().setJavaScriptEnabled(true);
                        frage_investdetail_text3.setWebChromeClient(new WebChromeClient());
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_fg_weidenglu:
                intent = new Intent(getActivity(), LoginActivity.class);
                intent.putExtra("state", "0");
                startActivity(intent);
        }
    }
}
